<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表情包</title>
    <style>
        .list {
            display: flex;
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="list">
        <!-- <div>
            <img src="" alt="">
            <div></div>
        </div> -->
    </div>
    <button class="prev">上一页</button>
    <button class="next">下一页</button>
    <span></span>/4
</body>

</html>
<script src="./ajax--promise封装.js"></script>
<script>
    // 保存当前页码
    var page = 1
    if (page = 1) {
        ajax('get', `https://yantianfeng.com/api/emotion`, { page: 1, count: 10 }).then((res) => {
            var str = res.emotions.map((item, index) => {
                return `<div>
            <img src="${item.url}" alt="">
            <div>${item.title}</div></div>`
            }).join('')
            // 渲染表情
            document.querySelector('.list').innerHTML = str

            document.querySelector('span').innerHTML = page

            document.querySelector('.prev').disabled = true
        })
    }
    // 点击上一页，获取上一页的表情并渲染表情
    function renderprev() {
        document.querySelector('.prev').onclick = function () {
            document.querySelector('.next').disabled = false
            if (page > 1) {
                ajax('get', `https://yantianfeng.com/api/emotion`, { page: --page, count: 10 }).then(() => {
                    var str = res.emotions.map((item, index) => {
                        return `<div>
            <img src="${item.url}" alt="">
            <div>${item.title}</div></div>`
                    }).join('')
                    // 渲染表情
                    document.querySelector('.list').innerHTML = str
                })
                if (page == 1) {
                    document.querySelector('.prev').disabled = true
                }
            }
            document.querySelector('span').innerHTML = page
        }
    }





    // 点击下一页，获取下一页的表情并渲染表情
    function rendernext() {
        document.querySelector('.next').onclick = function () {
            document.querySelector('.prev').disabled = false
            if (page < 4) {
                ajax('get', `https://yantianfeng.com/api/emotion`, { page: ++page, count: 10 }).then(() => {
                    var str = data.emotions.map((item, index) => {
                        return `<div>
            <img src="${item.url}" alt="">
            <div>${item.title}</div></div>`
                    }).join('')
                    // 渲染表情
                    document.querySelector('.list').innerHTML = str
                })
            }
            if (page == 4) {
                page = 4
            }
            document.querySelector('span').innerHTML = page
            if (page == 4) {
                document.querySelector('.next').disabled = true
            }
        }
    }
    renderprev()
    rendernext()
</script>